package com.snail.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.dto.VideoDataVo;
import com.snail.mapper.BizFilesMapper;
import com.snail.mapper.BizWorksMapper;
import com.snail.model.pojo.media.BizFiles;
import com.snail.model.pojo.media.BizWorks;
import com.snail.service.BizFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2025/8/18 16:37
 * @Author yangming
 * @Description biz_files表Service实现类
 **/
@Service
@DS("learn-media")
public class BizFilesServiceImpl extends ServiceImpl<BizFilesMapper, BizFiles> implements BizFilesService {
    @Autowired
    private BizWorksMapper bizWorksMapper;
    @Autowired
    private BizFilesMapper bizFilesMapper;

    @Override
    @DS("learn-media")
    public List<VideoDataVo> getRandomFilesByType0(Integer limit) {
        // 校验参数，避免limit为null或负数
        if (limit == null || limit <= 0) {
            limit = 10; // 默认查询10条
        }
        List<BizWorks> bizWorkss = bizWorksMapper.selectDistinctByFileMd5Random6();
        List<VideoDataVo> videoDataVos = new ArrayList<>();
        if (bizWorkss != null && !bizWorkss.isEmpty()) {
            for (BizWorks bizWorks : bizWorkss) {
                BizFiles file = bizFilesMapper.selectOne(new QueryWrapper<BizFiles>()
                        .eq("file_md5", bizWorks.getFileMd5())
                        .eq("file_type", 0));
                if (file != null) {
                    VideoDataVo videoDataVo = new VideoDataVo();
                    videoDataVo.setWorksName(bizWorks.getWorksName());
                    videoDataVo.setDescription(bizWorks.getDescription());
                    videoDataVo.setUserId(file.getUserId());
                    videoDataVo.setPreviewUrl(file.getPreviewUrl());
                    videoDataVo.setId(file.getId());
                    videoDataVos.add(videoDataVo);
                    if (videoDataVos.size() >= limit) {
                        break;
                    }
                }
            }
        }
        return videoDataVos;
    }
}
